// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: hyuan

#include "coin2/exchange/kucoin/api_order/native_rest_async_asio_subscriber.h"

namespace coin2::exchange::kucoin::api_order {

void KucoinRestAsyncAsioSubscriber::QueryBalance() {
  RestParams params;
  auto http_context = rest_client()->QueryAccountBalance();
  if (!http_context) return;
  http_context->handler =
      std::bind(&KucoinRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KucoinRestAsyncAsioSubscriber::QueryCrossBalance() {
  RestParams params;
  auto http_context = rest_client()->QueryAccountCrossBalance();
  if (!http_context) return;
  http_context->handler =
      std::bind(&KucoinRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KucoinRestAsyncAsioSubscriber::QueryOpenOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryOpenOrders(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&KucoinRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KucoinRestAsyncAsioSubscriber::QueryTransfer() {
  RestParams params;
  auto http_context = rest_client()->QueryTransfers(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&KucoinRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KucoinRestAsyncAsioSubscriber::QueryHistoryOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryHistoryOrders(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&KucoinRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KucoinRestAsyncAsioSubscriber::QueryFills(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryFills(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&KucoinRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KucoinRestAsyncAsioSubscriber::QueryBullet() {
  RestParams params;
  auto http_context = rest_client()->QueryBullet();
  if (!http_context) return;
  http_context->handler =
      std::bind(&KucoinRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void KucoinRestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(
      std::bind(&KucoinRestAsyncAsioSubscriber::QueryBullet, this),
      config_.query_account_balance_period_sec(),
      "query_bullet");

  AddPeriodicTask(
      std::bind(&KucoinRestAsyncAsioSubscriber::QueryBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance");

  if (is_readonly_order_system_) {
    AddPeriodicTask(
        std::bind(&KucoinRestAsyncAsioSubscriber::QueryCrossBalance, this),
        config_.query_account_balance_period_sec(),
        "query_cross_balance");
    AddPeriodicTask(
        std::bind(&KucoinRestAsyncAsioSubscriber::QueryTransfer, this),
        config_.query_account_balance_period_sec(),
        "query_transfer");
  }

  const auto symbol_list = native_symbols();
  AddPeriodicTask(
      std::bind(&KucoinRestAsyncAsioSubscriber::QueryOpenOrders, this, std::placeholders::_1),
      config_.query_open_orders_period_sec(),
      "query_open_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&KucoinRestAsyncAsioSubscriber::QueryHistoryOrders, this, std::placeholders::_1),
      config_.query_cancel_orders_period_sec(),
      "query_history_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&KucoinRestAsyncAsioSubscriber::QueryFills, this, std::placeholders::_1),
      config_.query_fill_period_sec(),
      "query_fills",
      symbol_list);
}

}  // namespace coin2::exchange::kucoin::api_order
